package com.binge.testThread;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

/**
 * ClassName:TestThread2
 * Package:com.binge.testThread
 * Description:
 *
 * @Author @Bin
 * @Date 2024/3/4-14:46
 * @Version: v1.0
 */

@Slf4j
public class TestThread2 {


    @Test
    public void test1() throws InterruptedException, IOException {
        Object o = new Object();


        Thread thread = new Thread(() -> {
            synchronized (o) {
                log.info("线程1开始");
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.info("线程1结束");
            }
        });

        thread.start();

        TimeUnit.SECONDS.sleep(2);

        synchronized (o) {
            log.info("thread state:{}", thread.getState());
            o.notify();
            TimeUnit.SECONDS.sleep(2);
            log.info("主线程结束");
        }


        System.in.read();


    }


    @Test
    public void test2() throws InterruptedException, IOException {

        Object o = new Object();

        Thread thread = new Thread(() -> {
            synchronized (o) {
                log.info("线程1开始");
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.info("线程1结束");
            }
        });

        thread.start();

        thread.join();

        TimeUnit.SECONDS.sleep(1);

        //调用子线程的interrupt方法

        synchronized (o) {
            thread.interrupt();
            System.in.read();
        }


    }


    @Test
    public void test3() throws InterruptedException {

        Object o = new Object();

        Thread thread = new Thread(() -> {
            synchronized (o) {
                log.info("线程1开始");
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.info("线程1结束");
            }
        });

        thread.start();

        Thread thread2 = new Thread(() -> {
            synchronized (o) {
                log.info("线程2开始");
                log.info("线程2结束");
            }
        });

        thread2.start();

        TimeUnit.SECONDS.sleep(1);

        log.info("thread2 state:{}", thread2.getState());
    }


    @Test
    public void test4() throws InterruptedException {

        Thread thread1 = new Thread(() -> {
            for (; ; ) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                    log.info("thread1 running");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        Thread mainThread = Thread.currentThread();

        Thread thread2 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
                mainThread.interrupt();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        thread1.start();

        thread2.start();

        TimeUnit.SECONDS.sleep(1);

        thread1.join();

        log.info("main thread end");
    }

    @Test
    public void test5() throws IOException {

        Thread thread = new Thread(() -> {
            for (; ; ) {
                try {
                    log.info("thread running");
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        thread.start();

        System.in.read();
    }

    @Test
    public void test6() {
        HashSet<String> set = new HashSet<>();
        set.add("1");
        boolean add = set.add("1");
        System.out.println(add);
    }


    @Test
    public void test7() {
        Integer a = 1;
        TestPerson testPerson = new TestPerson();
        //log.info("true".equalsIgnoreCase(testPerson.getName()) + "");
        //log.info(a.toString().toCharArray()+"");
        testPerson.getChildren().stream().forEach(System.out::println);

    }

    @Data
    class TestPerson {
        private String name;
        private int age;
        private List<TestPerson> children;;
    }
}
